package com.tf.temper.controller;

import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.temper.entity.*;
import com.tf.temper.service.LogeventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 统计
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "统计相关",description="StatisticsController")
@RestController
public class StatisticsController extends BaseController
{
    @Autowired
    private LogeventService<LogeventEntity> logeventService;

    @ApiOperation("人员统计-获取今日进入人数及离开人数")
    @GetMapping("/allPassInAndOutNum")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<StatisticsdateEntity> allPassInAndOutNum()
    {
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new  SimpleDateFormat(format);
        String curDate =  sdf.format(new Date());
        EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
        ew.eq("actioncode", 1);
        ew.between("createtime", curDate+" 00:00:00",curDate+" 23:59:59");
        int passInNum =  logeventService.selectCount(ew);
        EntityWrapperHandle<LogeventEntity> ewout = new EntityWrapperHandle<LogeventEntity>();
        ewout.eq("actioncode", 2);
        ewout.between("createtime", curDate+" 00:00:00",curDate+" 23:59:59");
        int outNum =  logeventService.selectCount(ewout);
        //logger.error("Error on  /StatisticsReturnEntity outNum==="+outNum);
        StatisticsdateEntity data = new StatisticsdateEntity();
        data.setpassInNum(passInNum);
        data.setoutNum(outNum);
        return  new StatisticsReturnEntity<StatisticsdateEntity>().ok(data);
    }

    @ApiOperation("人员统计-获取当日每小时内的进入及离开人数")
    @GetMapping("/everyHourPassInAndOutNum")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<StatisticsdateEntity[]> everyHourPassInAndOutNum()
    {
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new  SimpleDateFormat(format);
        String curDate =  sdf.format(new Date());
        //logger.error("Error on  /everyHourPassInAndOutNum curDate==="+curDate);
        Calendar calendar = Calendar.getInstance();
        int curhou24 = calendar.get(calendar.HOUR_OF_DAY);

        StatisticsdateEntity[] dataarr = new StatisticsdateEntity[curhou24+1];
        for(int i = 0;i<=curhou24;++i)
        {
            EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
            ew.eq("actioncode", 1);
            ew.between("createtime", curDate+" "+i+":00:00",curDate+" "+i+":59:59");
            int passInNum =  logeventService.selectCount(ew);
            EntityWrapperHandle<LogeventEntity> ewout = new EntityWrapperHandle<LogeventEntity>();
            ewout.eq("actioncode", 2);
            ewout.between("createtime", curDate+" "+i+":00:00",curDate+" "+i+":59:59");
            int outNum =  logeventService.selectCount(ewout);
            //logger.error("Error on  /everyHourPassInAndOutNum passInNum==="+passInNum+"  "+outNum);
            StatisticsdateEntity data = new StatisticsdateEntity();
            data.setpassInNum(passInNum);
            data.setoutNum(outNum);
            dataarr[i] = data;
        }
        //logger.error("Error on  /everyHourPassInAndOutNum curhou24==="+curhou24);
        return  new StatisticsReturnEntity<StatisticsdateEntity[]>().ok(dataarr);
    }

    @ApiOperation("今日统计-获取员工正常及异常人数")
    @GetMapping("/employeeNormalAndAbnormalNum")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<StatisticspeoplenumEntity> employeeNormalAndAbnormalNum()
    {
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new  SimpleDateFormat(format);
        String curDate =  sdf.format(new Date());
        EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
        ew.eq("usertype", "student");
        ew.eq("opencode", 1);
        ew.between("createtime", curDate+" 00:00:00",curDate+" 23:59:59");
        int normalNum =  logeventService.selectCount(ew);
        EntityWrapperHandle<LogeventEntity> ewout = new EntityWrapperHandle<LogeventEntity>();
        ewout.eq("usertype", "student");
        ewout.eq("opencode", 0);
        ewout.between("createtime", curDate+" 00:00:00",curDate+" 23:59:59");
        int abnormalNum =  logeventService.selectCount(ewout);
        logger.error("Error on  /StatisticsReturnEntity normalNum==="+normalNum+"   "+abnormalNum);
        StatisticspeoplenumEntity data = new StatisticspeoplenumEntity();
        data.setnormalNum(normalNum);
        data.setabnormalNum(abnormalNum);
        return  new StatisticsReturnEntity<StatisticspeoplenumEntity>().ok(data);
    }

    @ApiOperation("今日统计-获取访客正常及异常人数")
    @GetMapping("/visitorNormalAndAbnormalNum")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<StatisticspeoplenumEntity> visitorNormalAndAbnormalNum()
    {
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new  SimpleDateFormat(format);
        String curDate =  sdf.format(new Date());
        EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
        ew.eq("usertype", "user");
        ew.eq("opencode", 1);
        ew.between("createtime", curDate+" 00:00:00",curDate+" 23:59:59");
        int normalNum =  logeventService.selectCount(ew);
        EntityWrapperHandle<LogeventEntity> ewout = new EntityWrapperHandle<LogeventEntity>();
        ewout.eq("usertype", "user");
        ewout.eq("opencode", 0);
        ewout.between("createtime", curDate+" 00:00:00",curDate+" 23:59:59");
        int abnormalNum =  logeventService.selectCount(ewout);
        logger.error("Error on  /StatisticsReturnEntity normalNum==="+normalNum+"   "+abnormalNum);
        StatisticspeoplenumEntity data = new StatisticspeoplenumEntity();
        data.setnormalNum(normalNum);
        data.setabnormalNum(abnormalNum);
        return  new StatisticsReturnEntity<StatisticspeoplenumEntity>().ok(data);
    }

    @ApiOperation("获取设备状态统计信息")
    @GetMapping("/deviceStatusStatistics")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<StatisticsdeviceEntity> deviceStatusStatistics()
    {
        StatisticsdeviceEntity data = new StatisticsdeviceEntity();
        data.setOnlineNormalNum(1);
        data.setOnlineAbnormalNum(0);
        data.setOfflineNum(0);
        return  new StatisticsReturnEntity<StatisticsdeviceEntity>().ok(data);
    }

    @ApiOperation("获取口罩佩戴异常统计列表")
    @GetMapping("/respiratorAbnormalWearing")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<StatisticsmaskEntity[]> respiratorAbnormalWearing(@RequestParam int size,@RequestParam int page)
    {
        EntityWrapperHandle<LogeventEntity> ewcount = new EntityWrapperHandle<LogeventEntity>();
        int count = logeventService.selectCount(ewcount);
        if(page>=1&&size>0&&size<=200&&count>0)
        {
            if( (count -  (page-1)*size) >0 )
            {//logger.error("Error on  /respiratorAbnormalWearing normalNum==="+size+"   "+size);
                EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
                ew.orderDesc(Arrays.asList(new String[] {"id"}));
                ew.last("limit "+(page-1)*size+","+size);
                List<LogeventEntity> normalNum =  logeventService.selectList(ew);
                StatisticsmaskEntity[] data = null;
                if(size>0&&size<=200&&normalNum!=null)
                {
                    String format = "yyyy-MM-dd HH:mm:ss";
                    SimpleDateFormat sdf = new  SimpleDateFormat(format);
                    int maxlen = 0;
                    if(size>=normalNum.size())
                    {
                        maxlen = normalNum.size();
                    }
                    else
                    {
                        maxlen = size;
                    }
                    data = new StatisticsmaskEntity[maxlen];
                    for (int i= 0;i<maxlen;++i)
                    {
                        StatisticsmaskEntity dataval = new StatisticsmaskEntity();
                        dataval.setId(normalNum.get(i).getId());
                        dataval.setDeviceId(normalNum.get(i).getDeviceid());
                        dataval.setImgUrl(normalNum.get(i).getImgurl());
                        dataval.setName(normalNum.get(i).getUserName());
                        dataval.setStatus(normalNum.get(i).getMask());
                        dataval.setTime(sdf.format(normalNum.get(i).getCreatetime()));
                        data[i] = dataval;
                    }
                }
                else
                {
                    data = new StatisticsmaskEntity[1];
                }
                return  new StatisticsReturnEntity<StatisticsmaskEntity[]>().ok(data,count);
            }
        }
        return  new StatisticsReturnEntity<StatisticsmaskEntity[]>().error("size or page err");
    }

    @ApiOperation("比对记录查询列表")
    @GetMapping("/comparisonLog")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<ComparisonlogreturnEntity[]> comparisonLog(@RequestParam Map<String, Object> data)
    {//logger.error("Error on  /respiratorAbnormalWearing data==="+data+"   "+data.size());
        if(data!=null&&data.size()>0)
        {
            int size = Integer.valueOf((String) data.get("size"));
            int page = Integer.valueOf((String) data.get("page"));
            EntityWrapperHandle<LogeventEntity> ewcount = new EntityWrapperHandle<LogeventEntity>();
            int count = logeventService.selectCount(ewcount);
            if(page>=1&&size>0&&size<=200&&count>0)
            {
                if( (count -  (page-1)*size) >0 )
                {
                    EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
                    ew.orderDesc(Arrays.asList(new String[] {"id"}));
                    ewcount.orderDesc(Arrays.asList(new String[] {"id"}));
                    ew.last("limit "+(page-1)*size+","+size);
                    String saveTimeStart = (String)data.get("saveTimeStart");
                    String saveTimeEnd = (String)data.get("saveTimeEnd");
                    if(saveTimeStart!=null||saveTimeEnd!=null)
                    {
                        ew.between("createtime", saveTimeStart,saveTimeEnd);
                        ewcount.between("createtime", saveTimeStart,saveTimeEnd);
                    }
                    Object temperatureStart =  data.get("temperatureStart");
                    Object temperatureEnd = data.get("temperatureEnd");
                    if(temperatureStart!=null||temperatureEnd!=null)
                    {
                        ew.between("temperature", temperatureStart,temperatureEnd);
                        ewcount.between("temperature", temperatureStart,temperatureEnd);
                    }
                    Object comparisonStatus = data.get("comparisonStatus");
                    logger.error("Error on  /respiratorAbnormalWearing comparisonStatus==="+comparisonStatus+"   ");
                    if(comparisonStatus!=null)
                    {
                        int comparisonStatusVal = Integer.valueOf((String) data.get("comparisonStatus"));
                        logger.error("Error on  /respiratorAbnormalWearing comparisonStatusVal==="+comparisonStatusVal+"   ");
                        if(comparisonStatusVal==1)
                        {
                            ew.eq("usertype","student");
                            ewcount.eq("usertype","student");
                        }
                        else if(comparisonStatusVal ==2)
                        {
                            ew.eq("usertype","user");
                            ewcount.eq("usertype","user");
                        }
                    }
                    Object name = data.get("name");
                    if(name!=null)
                    {
                        ew.like("username",(String) name);
                        ewcount.like("username",(String) name);
                    }
                    Object deviceName = data.get("deviceName");
                    if(deviceName!=null)
                    {
                        ew.like("deviceid",(String) deviceName);
                        ewcount.like("deviceid",(String) deviceName);
                    }
                    List<LogeventEntity> normalNum =  logeventService.selectList(ew);
                    count = logeventService.selectCount(ewcount);
                    //logger.error("Error on  /respiratorAbnormalWearing normalNum==="+ normalNum.get(1).getId()+"   "+normalNum.size());
                    ComparisonlogreturnEntity[] datareturn = null;
                    if(size>0&&size<=200&&normalNum!=null)
                    {
                        String format = "yyyy-MM-dd HH:mm:ss";
                        SimpleDateFormat sdf = new  SimpleDateFormat(format);
                        int maxlen = 0;
                        if(size>=normalNum.size())
                        {
                            maxlen = normalNum.size();
                        }
                        else
                        {
                            maxlen = size;
                        }
                        datareturn = new ComparisonlogreturnEntity[maxlen];
                        for (int i= 0;i<maxlen;++i)
                        {
                            ComparisonlogreturnEntity dataval = new ComparisonlogreturnEntity();
                            dataval.setId(normalNum.get(i).getId());
                            dataval.setName(normalNum.get(i).getUserName());
                            dataval.setTemperature(normalNum.get(i).getTemperature());
                            dataval.setIdentityId(normalNum.get(i).getCardid());
                            dataval.setSex(normalNum.get(i).getSex());
                            dataval.setComparisonValue(normalNum.get(i).getSimilarity());
                            if(normalNum.get(i).getUserType()==null)
                            {
                                dataval.setComparisonStatus("2");
                            }
                            else
                            {
                                dataval.setComparisonStatus(normalNum.get(i).getUserType().equals("student")?"1":"2");
                            }
                            dataval.setRespiratorStatus(normalNum.get(i).getMask());
                            dataval.setSaveTime(sdf.format(normalNum.get(i).getCreatetime()));
                            dataval.setDeviceId(normalNum.get(i).getDeviceid());
                            dataval.setDeviceName(normalNum.get(i).getNationCode());
                            dataval.setTemplateImgUrl(normalNum.get(i).getTemplateimgurl());
                            dataval.setLocaleImgUrl(normalNum.get(i).getImgurl());
                            datareturn[i] = dataval;
                        }
                    }
                    else
                    {
                        datareturn = new ComparisonlogreturnEntity[1];
                    }
                    return  new StatisticsReturnEntity<ComparisonlogreturnEntity[]>().ok(datareturn,count);
                }
            }
        }
        return  new StatisticsReturnEntity<ComparisonlogreturnEntity[]>().error("size or page err");
    }

    @ApiOperation("获取温度信息相关的接口")
    @GetMapping("/transmittemperature")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<TransmittemperaturereturnEntity[]> transmittemperature(@RequestParam Map<String, Object> data)
    {
        if(data!=null&&data.size()>0)
        {
            int size = 20;int page = 1;
            EntityWrapperHandle<LogeventEntity> ewcount = new EntityWrapperHandle<LogeventEntity>();
            int count = logeventService.selectCount(ewcount);
            if(count>0)
            {
                if( (count -  (page-1)*size) >0 )
                {
                    EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
                    ew.orderDesc(Arrays.asList(new String[] {"id"}));
                    ewcount.orderDesc(Arrays.asList(new String[] {"id"}));
                    ew.last("limit "+(page-1)*size+","+size);
                    String saveTimeStart = (String)data.get("timeStart");
                    String saveTimeEnd = (String)data.get("timeEnd");
                    if(saveTimeStart!=null||saveTimeEnd!=null)
                    {
                        ew.between("createtime", saveTimeStart,saveTimeEnd);
                        ewcount.between("createtime", saveTimeStart,saveTimeEnd);
                    }
                    Object temperatureStart =  data.get("temperatureStart");
                    Object temperatureEnd = data.get("temperatureEnd");
                    if(temperatureStart!=null||temperatureEnd!=null)
                    {
                        ew.between("temperature", temperatureStart,temperatureEnd);
                        ewcount.between("temperature", temperatureStart,temperatureEnd);
                    }
                    Object comparisonStatus = data.get("comparisonStatus");
                    if(comparisonStatus!=null)
                    {
                        int comparisonStatusVal = Integer.valueOf((String) data.get("comparisonStatus"));
                        if(comparisonStatusVal==1)
                        {
                            ew.eq("usertype","student");
                            ewcount.eq("usertype","student");
                            logger.info("info on  /transmittemperature comparisonStatusVal===1");
                        }
                        else if(comparisonStatusVal ==2)
                        {
                            ew.eq("usertype","user");
                            logger.info("info on  /transmittemperature comparisonStatusVal===2");
                            ewcount.eq("usertype","user");
                        }
                    }
                    Object name = data.get("name");
                    if(name!=null)
                    {
                        ew.like("username",(String) name);
                        ewcount.like("username",(String) name);
                    }
                    Object deviceName = data.get("deviceName");
                    if(deviceName!=null)
                    {
                        ew.like("deviceid",(String) deviceName);
                        ewcount.like("deviceid",(String) deviceName);
                    }
                    List<LogeventEntity> normalNum =  logeventService.selectList(ew);
                    count = logeventService.selectCount(ewcount);
                    TransmittemperaturereturnEntity[] datareturn = null;
                    if(normalNum!=null)
                    {
                        String format = "yyyy-MM-dd HH:mm:ss";
                        SimpleDateFormat sdf = new  SimpleDateFormat(format);
                        int maxlen = 0;
                        if(size>=normalNum.size())
                        {
                            maxlen = normalNum.size();
                        }
                        else
                        {
                            maxlen = size;
                        }
                        logger.info("info on  /transmittemperature maxlen==="+maxlen);
                        datareturn = new TransmittemperaturereturnEntity[maxlen];
                        for (int i= 0;i<maxlen;++i)
                        {
                            TransmittemperaturereturnEntity dataval = new TransmittemperaturereturnEntity();
                            dataval.setId(normalNum.get(i).getId());
                            dataval.setName(normalNum.get(i).getUserName());
                            dataval.setTemperature(normalNum.get(i).getTemperature());
                            logger.info("info on  /transmittemperature dataval==="+dataval);
                            dataval.setRespiratorStatus(normalNum.get(i).getMask());
                            dataval.setSaveTime(sdf.format(normalNum.get(i).getCreatetime()));
                            dataval.setDeviceId(normalNum.get(i).getDeviceid());
                            dataval.setDeviceName(normalNum.get(i).getNationCode());
                            dataval.setLocaleImgUrl(normalNum.get(i).getImgurl());
                            datareturn[i] = dataval;
                        }
                    }
                    else
                    {
                        datareturn = new TransmittemperaturereturnEntity[1];
                    }
                    return  new StatisticsReturnEntity<TransmittemperaturereturnEntity[]>().ok(datareturn,count);
                }
            }
        }
        return  new StatisticsReturnEntity<TransmittemperaturereturnEntity[]>().error("size or page err");
    }

    @ApiOperation("获取人脸信息相关的接口")
    @GetMapping("/transmitsyface")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<TransmitsyfacereturnEntity[]> transmitsyface(@RequestParam Map<String, Object> data)
    {
        if(data!=null&&data.size()>0)
        {
            int size = 20;int page = 1;
            EntityWrapperHandle<LogeventEntity> ewcount = new EntityWrapperHandle<LogeventEntity>();
            int count = logeventService.selectCount(ewcount);
            logger.info("info on  /transmitsyface count==="+count);
            if(count>0)
            {
                if( (count -  (page-1)*size) >0 )
                {
                    EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
                    ew.orderDesc(Arrays.asList(new String[] {"id"}));
                    ewcount.orderDesc(Arrays.asList(new String[] {"id"}));
                    ew.last("limit "+(page-1)*size+","+size);
                    String saveTimeStart = (String)data.get("timeStart");
                    String saveTimeEnd = (String)data.get("timeEnd");
                    logger.info("info on  /transmitsyface saveTimeStart==="+saveTimeStart);
                    if(saveTimeStart!=null||saveTimeEnd!=null)
                    {
                        ew.between("createtime", saveTimeStart,saveTimeEnd);
                        ewcount.between("createtime", saveTimeStart,saveTimeEnd);
                    }
                    Object temperatureStart =  data.get("temperatureStart");
                    Object temperatureEnd = data.get("temperatureEnd");
                    if(temperatureStart!=null||temperatureEnd!=null)
                    {
                        ew.between("temperature", temperatureStart,temperatureEnd);
                        ewcount.between("temperature", temperatureStart,temperatureEnd);
                    }
                    Object comparisonStatus = data.get("comparisonStatus");
                    if(comparisonStatus!=null)
                    {
                        int comparisonStatusVal = Integer.valueOf((String) data.get("comparisonStatus"));
                        if(comparisonStatusVal==1)
                        {
                            logger.info("info on  /transmitsyface comparisonStatusVal===1");
                            ew.eq("usertype","student");
                            ewcount.eq("usertype","student");
                        }
                        else if(comparisonStatusVal ==2)
                        {
                            logger.info("info on  /transmitsyface comparisonStatusVal===2");
                            ew.eq("usertype","user");
                            ewcount.eq("usertype","user");
                        }
                    }
                    Object name = data.get("name");
                    if(name!=null)
                    {
                        ew.like("username",(String) name);
                        ewcount.like("username",(String) name);
                    }
                    Object deviceName = data.get("deviceName");
                    if(deviceName!=null)
                    {
                        ew.like("deviceid",(String) deviceName);
                        ewcount.like("deviceid",(String) deviceName);
                    }
                    List<LogeventEntity> normalNum =  logeventService.selectList(ew);
                    count = logeventService.selectCount(ewcount);
                    TransmitsyfacereturnEntity[] datareturn = null;
                    if(normalNum!=null)
                    {
                        String format = "yyyy-MM-dd HH:mm:ss";
                        SimpleDateFormat sdf = new  SimpleDateFormat(format);
                        int maxlen = 0;
                        if(size>=normalNum.size())
                        {
                            maxlen = normalNum.size();
                        }
                        else
                        {
                            maxlen = size;
                        }
                        logger.info("info on  /transmitsyface maxlen==="+maxlen);
                        datareturn = new TransmitsyfacereturnEntity[maxlen];
                        for (int i= 0;i<maxlen;++i)
                        {
                            TransmitsyfacereturnEntity dataval = new TransmitsyfacereturnEntity();
                            dataval.setId(normalNum.get(i).getId());
                            dataval.setName(normalNum.get(i).getUserName());
                            dataval.setTemperature(normalNum.get(i).getTemperature());
                            logger.info("info on  /transmitsyface dataval==="+dataval);
                            dataval.setRespiratorStatus(normalNum.get(i).getMask());
                            dataval.setSaveTime(sdf.format(normalNum.get(i).getCreatetime()));
                            dataval.setDeviceId(normalNum.get(i).getDeviceid());
                            dataval.setDeviceName(normalNum.get(i).getNationCode());
                            dataval.setLocaleImgUrl(normalNum.get(i).getImgurl());
                            datareturn[i] = dataval;
                        }
                    }
                    else
                    {
                        datareturn = new TransmitsyfacereturnEntity[1];
                    }
                    return  new StatisticsReturnEntity<TransmitsyfacereturnEntity[]>().ok(datareturn,count);
                }
            }
        }
        return  new StatisticsReturnEntity<TransmitsyfacereturnEntity[]>().error("size or page err");
    }
}